package com.ssm.controller;

import com.ssm.pojo.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: exercise1_online_shopping
 * @author: qin haojun
 * @create: 2021-02-14 18:21
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户登录
     * @param uname
     * @param password
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/logining",method = {RequestMethod.POST})
    public String login(@RequestParam(value = "uname") String uname,
                        @RequestParam(value = "password") String password,
                        HttpServletRequest request)
            throws Exception {
//        ModelAndView modelAndView = new ModelAndView();

        //下面是如果要登录
        //如果用户名或密码错误，那么返回登录界面
        if (!userService.login(uname, password)) {
//            modelAndView.setViewName("redirect:login");
            User user = new User();
            user.setPassword(password);
            user.setUname(uname);
            request.getSession().setAttribute("status","用户名或密码输入错误");
            request.getSession().setAttribute("uname",uname);
            request.getSession().setAttribute("password",password);
            return "redirect:login";
        }

        //登陆成功，回到主页面
        User user = userService.getUserByPwd(password);
        request.getSession().setAttribute("user", user);
//        modelAndView.setViewName("redirect:/index.jsp");
        return "redirect:/index.jsp";

    }
    //
    @RequestMapping("/logout")
    public String login(HttpServletRequest request)
            throws Exception {
        request.getSession().invalidate();
        return "redirect:/index.jsp";
    }
//
    //1表示成功，-1表示密码重叠，0表示用户名重叠
    @RequestMapping("/registering")
    public @ResponseBody Integer register(User user)throws Exception{
        return userService.addUser(user);
    }

    //下面是一些简单地跳转页面操作
    @RequestMapping("/myinf")
    public String myinf()throws Exception{
        return "user/information";
    }

    @RequestMapping("/login")
    public String login() throws Exception{
        return "user/login";
    }

    @RequestMapping("/register")
    public String register() throws Exception{
        return "user/register";
    }

    @RequestMapping(value = "/updateName",method = {RequestMethod.POST})
    public String updateUname(@RequestParam(value = "old_uid")Integer oldUid,
                              @RequestParam("new_uid")Integer newUid,
                              HttpServletRequest request) throws Exception {
        //如果已经有人用了这个id，不能更改用户名，直接返回
        if(userService.getUserById(newUid)!=null){
            return "user/information";
        }
        User user=(User)request.getSession().getAttribute("user");
        user.setUid(newUid);
        userService.updateId(oldUid,newUid,user);
        //如果成功的话，更新会话里的user
        request.getSession().setAttribute("user",user);
        return "user/information";
    }
}
